﻿using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class OrderService
    {
        public static List<Order> GetOrders(int CustId)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Order.Where(p => p.CustId == CustId && p.IsTake == "未发货" || p.IsTake == "已发货" || p.IsTake== "未支付").ToList();
            }
        }
        public static Order SelectId(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                return db.Order.FirstOrDefault(p => p.Id == id);
            }
        }

        public static int UpdateIsTake(string OrderId)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string sql = $"Update [Order] set IsTake ='未发货' where OrderId ={OrderId} ";
                return db.Database.ExecuteSqlCommand(sql);
            }
        }
        /// <summary>
        /// 添加订单信息
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public static int AddOrderMsg(Order order)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                db.Order.Add(order);

                return db.SaveChanges();
            }
        }

        public static int UpdateOrder(int Id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Order order = db.Order.FirstOrDefault(p => p.Id == Id);
                order.IsTake = "已收货";
                return db.SaveChanges();
            }
        }

        /// <summary>
        /// 获取订单信息
        /// </summary>
        /// <param name="limit"></param>
        /// <param name="page"></param>
        /// <param name="Name"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public static List<Order> GetOrderMes(int limit, int page, string OrderId, string IsTake, out int total)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                var query = db.Order.Where(p => true);
                if (!string.IsNullOrEmpty(OrderId))
                {
                    query = query.Where(p => p.OrderId.Contains(OrderId));
                }
                if (!string.IsNullOrEmpty(IsTake))
                {
                    query = query.Where(p => p.IsTake == IsTake);

                }
                total = query.Count();
                return query.OrderBy(p => p.Id).Skip((page - 1) * limit).Take(limit).ToList();
            }
        }
        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public static int GetBatchDelByIds(string Ids)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string newids = Ids.Substring(0, Ids.Length - 1);
                string sql = $"delete [Order] where Id in ({newids})";
                return db.Database.ExecuteSqlCommand(sql);
            }

        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static int GetArtDel(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string sql = $"delete [Order] where Id ={id}";
                return db.Database.ExecuteSqlCommand(sql);
            }
        }

        /// <summary>
        /// 批量发货
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        public static int GetDFaHuos(string Ids)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string newids = Ids.Substring(0, Ids.Length - 1);
                string sql = $"Update [Order] set IsTake ='已发货' where Id in ({newids}) ";
                return db.Database.ExecuteSqlCommand(sql);
            }
        }
        public static int GetDFaHuo(int id)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                string sql = $"Update [Order] set IsTake ='已发货' where Id ={id} ";
                return db.Database.ExecuteSqlCommand(sql);
            }
        }

        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="order"></param>
        /// <returns></returns>
        public static int AddOrder(Order order,int ProductId1,int Count)
        {
            using (ShopDemoContextt db = new ShopDemoContextt())
            {
                Order order1 = new Order
                {
                    OrderId = order.OrderId,
                    CustId = Convert.ToInt32(order.CustId),
                    ProductName = order.ProductName,
                    CustName = order.CustName,
                    ShouName = order.ShouName,
                    ToPrice = Convert.ToDecimal(order.ToPrice),
                    IsTake = order.IsTake,
                    OrderTime = order.OrderTime,
                    ProductPrth=order.ProductPrth,
                    Count= Count,
                    Price=order.Price

                };
                db.Order.Add(order1);

                string ProductId = ProductId1.ToString();

                var a = OrderDetailService.GetOrderDetailMsg(ProductId);

                OrderDetail orderDetail = new OrderDetail
                {
                    OrderId = order.OrderId,
                    CustName=order.CustName,
                    CustId= Convert.ToInt32(order.CustId),
                    ProductId = ProductId1,
                    ProductName=a.ProductName,
                    ProductPrth=a.ProductImg,
                    Count= Count,
                    Price = a.Price,
                    //ToPrice= Convert.ToDecimal(order.ToPrice)


                };
                db.OrderDetail.Add(orderDetail);
                return db.SaveChanges();

            }
        }

    }
}
